package by.semerikov.data;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;

import by.semerikov.exceptions.ConnectorException;



public class Connector {
	private static DataSource ds;
	static
	{
		PoolProperties p = new PoolProperties();
		 p.setUrl("jdbc:mysql://localhost:3306/transportsystem");
		 p.setDriverClassName("com.mysql.jdbc.Driver");
		 p.setUsername("root");
		 p.setPassword("root");
		 p.setJmxEnabled(true);
		 p.setTestWhileIdle(false);
		 p.setTestOnBorrow(true);
		 p.setValidationQuery("SELECT 1");
		 p.setTestOnReturn(false);
		 p.setValidationInterval(30000);
		 p.setTimeBetweenEvictionRunsMillis(30000);
		 p.setMaxActive(100);
		 p.setInitialSize(10);
		 p.setMaxWait(10000);
		 p.setRemoveAbandonedTimeout(60);
		 p.setMinEvictableIdleTimeMillis(30000);
		 p.setMinIdle(10);
		 p.setLogAbandoned(true);
		 p.setRemoveAbandoned(true);
		 p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"+
		 "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
		 ds = new org.apache.tomcat.jdbc.pool.DataSource();
		 ds.setPoolProperties(p);
	}
	public static Connection getConnection() throws ConnectorException 
	{
			try {
				return ds.getConnection();
			} catch (SQLException e) {
				throw new ConnectorException("exception.connector.getconnection",e);
			}
	}
	 
}

